//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
/*
 * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Thomas Roell not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Thomas Roell makes no representations
 * about the suitability of this software for any purpose.  It is provided
 * "as is" without express or implied warranty.
 *
 * THOMAS ROELL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THOMAS ROELL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */
#include <ddk.h>
#include <osenv.h>
#include <math.h>

#define NEED_EVENTS
#include "X.h"
#include "Xmd.h"
#include "Xproto.h"

#include "scrnintstr.h"

#include "compiler.h"

#include "xf86.h"
#include "xf86Procs.h"
#include "xf86_OSlib.h"
#include "gdivga.h"

#define PATH_MAX    1024


/* xf86Exiting is set while the screen is shutting down (even on a reset) */
Boolean xf86Exiting = FALSE;
Boolean xf86Resetting = FALSE;
Boolean xf86ProbeFailed = TRUE;
Boolean xf86FlipPixels = FALSE;
#ifdef XF86VIDMODE
Boolean xf86VidModeEnabled = TRUE;
Boolean xf86VidModeAllowNonLocal = FALSE;
#endif
#ifdef XF86MISC
Boolean xf86MiscModInDevEnabled = TRUE;
Boolean xf86MiscModInDevAllowNonLocal = FALSE;
#endif
Boolean xf86AllowMouseOpenFail = FALSE;
PciProbeType xf86PCIFlags = PCIProbe1;
Boolean xf86ScreensOpen = FALSE;
int xf86Verbose = 1;
Boolean xf86fpFlag = FALSE;
Boolean xf86coFlag = FALSE;
Boolean xf86sFlag = FALSE;
Boolean xf86ProbeOnly = FALSE;
char xf86ConfigFile[PATH_MAX] = "";
int  xf86bpp = -1;
xrgb xf86weight = { 0, 0, 0 } ;    /* RGB weighting at 16 bpp */
//double xf86rGamma=1.0, xf86gGamma=1.0, xf86bGamma=1.0;
unsigned char xf86rGammaMap[256], xf86gGammaMap[256], xf86bGammaMap[256];
char *xf86ServerName = NULL;
Boolean xf86BestRefresh = FALSE;

int   vgaIOBase = 0x3d0;
int   vgaCRIndex = 0x3d4;
int   vgaCRReg = 0x3d5;
#if 0//not used
static void xf86PrintBanner(
    void
);
#endif
static void xf86PrintConfig(
    void
);


extern ScrnInfoPtr xf86Screens[];
extern int xf86MaxScreens;

xf86InfoRec xf86Info;
int         xf86ScreenIndex;


extern int Set_LogicalWidth(DisplayModePtr mode);
/*
 * InitOutput --
 * Initialize screenInfo for all actually accessible framebuffers.
 *      That includes vt-manager setup, querying all possible devices and
 *      collecting the pixmap formats.
 */

void
InitOutput(ScreenInfo *pScreenInfo)
{
    int                    i;//, j = 0, scr_index=0;
//    static int             numFormats = 0;
//    static PixmapFormatRec formats[MAXFORMATS];
    static unsigned long   generation = 0;
//    int                    any_screens = 0;

    if (serverGeneration == 1) {
        xf86PrintConfig();

        xf86OpenConsole();

        for (i=0; i<256; i++) {
//            xf86rGammaMap[i] = (int)(pow(i/255.0,xf86rGamma)*255.0+0.5);
//            xf86gGammaMap[i] = (int)(pow(i/255.0,xf86gGamma)*255.0+0.5);
//            xf86bGammaMap[i] = (int)(pow(i/255.0,xf86bGamma)*255.0+0.5);
            xf86rGammaMap[i] = i;
            xf86gGammaMap[i] = i;
            xf86bGammaMap[i] = i;
        }

        xf86Config(TRUE); /* Probe displays, and resolve modes */

    } else {
        /*
         * serverGeneration != 1; some OSs have to do things here, too.
         */
        xf86OpenConsole();
    }

    if (generation != serverGeneration){
        xf86ScreenIndex = AllocateScreenPrivateIndex();
        generation = serverGeneration;
    }
}

void  Screen_Init(DisplayModePtr mode)
{
    int i, scr_index;
    char *p[1];

    p[0]= (char *)mode;

    Set_LogicalWidth(mode);

    for ( i=0; i < xf86MaxScreens && xf86Screens[i] &&
             xf86Screens[i]->configured; i++ ) {
        /*
         * On a server-reset, we have explicitely to remap all stuff ...
         * (At startuptime this is implicitely done by probing the device
         */
        if (serverGeneration != 1){
            xf86Resetting = TRUE;
            xf86Exiting = FALSE;
            xf86VTSema = TRUE;
            (xf86Screens[i]->EnterLeaveVT)(ENTER, i);
            xf86Resetting = FALSE;
        }

        scr_index = AddScreen(xf86Screens[i]->Init, 0, p);

        if (scr_index > -1) {
            screenInfo.screens[scr_index]->devPrivates[xf86ScreenIndex].ptr
                = (pointer)xf86Screens[i];
        }
    }
}


static void
xf86PrintConfig()
{
    int i;

    for (i = 0; i < xf86MaxScreens; i++) {
        if (xf86Screens[i]) {
            (xf86Screens[i]->PrintIdent)();
        }
    }
}

